<?php


namespace app\model;

use think\facade\Db;
use think\Model;
use app\model\QuotationModel;
class QuotationTransportModel extends Model
{
    protected $table = "store_quotation_transport";

    /**
     * @param $set
     * @return QuotationTransportModel[]|array|\think\Collection
     * @notes 运输设置
     * @author: dongdong
     * @date: 2025/1/10 18:35
     */
    public function transport_set($set){
        try {
            return self::where("id","in",$set)->select();
        }catch (\Exception $e){
            return [];
        }
    }


    /**
     * @param $param
     * @return int
     * @notes 运输报价
     * @author: dongdong
     * @date: 2025/1/14 16:32
     */
    public function transport_quotation($param){
        Db::startTrans();
        try {
            $car_ids = $param['car_ids']??'';
            $str = '';

            $QuotationModel = new QuotationModel();

            $find = $QuotationModel->where(['user_id'=>$param['user_id'],'delivery_id'=>$param['delivery_id'],'is_deleted'=>0])->find();
            if(!empty($find)){
                Db::commit();
                return '您已报过价啦,请不要重复报价';
            }


            if($car_ids){
                $arr = explode(',',$car_ids);
                $price_arr = explode(',',$param['price']);
                $price_unit_arr = explode(',',$param['price_unit']);
                $validity_arr = explode(',',$param['validity']);
                $delivery_at_arr = explode(',',$param['delivery_at']);
                $quotation_end_arr = explode(',',$param['quotation_end_at']);

                $d = [];
                foreach ($arr as $k =>&$v){
                    $d['delivery_id'] = $param['delivery_id'];
                    $d['user_id'] = $param['user_id'];
                    $d['car_id'] = $v;
                    $d['price'] = $price_arr[$k]??'';
                    $d['price_unit'] = $price_unit_arr[$k]??'';
                    $d['validity'] = $validity_arr[$k]??'';
                    $d['delivery_at'] = $delivery_at_arr[$k]??'';
                    $d['create_at'] = date('Y-m-d H:i:s');
                    $d['quotation_end_at'] = $quotation_end_arr[$k]??'';
                    $insertGetId = self::insertGetId($d);
                    $str .= $insertGetId.',';
                }
                $str = trim($str,',');

                $dd = $d;
                unset($dd['car_id']);
                unset($dd['delivery_at']);
                $dd['transport_set'] = $str;
                $dd['is_transport'] = 1;
                $dd['quotation_end_at'] = $quotation_end_arr[0];

                $count = count($arr);
                $delivery_id = $param['delivery_id'];
                Db::query("update store_delivery_attach set quote_number=quote_number+$count where delivery_id=$delivery_id");
                $QuotationModel->insert($dd);
            }
            Db::commit();
            return 1;
        }catch (\Exception $e){
            _d($e);
            Db::rollback();
            return '接口错误';
        }
    }



    /**
     * @param $delivery_id
     * @param $user_id
     * @return QuotationModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 运输报价
     * @author: dongdong
     * @date: 2025/2/21 17:59
     */
    public function quotation_info($delivery_id,$user_id){
        $list = self::where("delivery_id=$delivery_id and user_id=$user_id and is_deleted=0")->select();
        $CarModel = new CarModel();
        foreach ($list as $k =>&$v){
            $v['car'] = $CarModel->find($v['car_id']);
            if($v['car']){
                $v['car']['car_category_name'] = $CarModel->car_category_name($v['car']['car_category_id']);
            }
        }
        return $list;
    }


}